package
{
	import EventDispatchers.XMLLoader;
	
	import Events.XMLEvent;
	
	import Graphics.TileSheet;
	
	import Menus.*;
	import Managers.MenuManager;
	import Utils.Camera;
	import Utils.Key;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class Game
	{
		private var topClass:TOI;
		
		[Embed(source='Data/Menus/MainMenu.png')]
		private var menuImage:Class;
		[Embed(source='Data/Menus/PauseTest.png')]
		private var pause:Class;
		
		private var backGroundBitmapData:BitmapData;
		private var canvasBitmapData:BitmapData;
		private var canvasBitmap:Bitmap;
		
		private var levels:Array = new Array();
		private var currentLevel:Level;
		private var play:Boolean = false;
		
		private var hero:Player;
		
		private var camera:Camera;
		
		private var isPaused:Boolean = true;
		private var isGameOver:Boolean = false;
		private var menuManager:MenuManager;
		
		public function get IsPaused():Boolean
		{
			return isPaused;
		}	
		public function set IsGameOver(value:Boolean):void
		{
			isGameOver = value;
		}
		public function get CurrentLevel():Level
		{
			return currentLevel;
		}

		public function get CanvasBitmapData():BitmapData
		{
			return canvasBitmapData;
		}
		
		public function get TopClass():TOI
		{
			return topClass;
		}
		
		public function get Hero():Player
		{
			return hero;
		}
		
		public function Game(toi:TOI)
		{
			topClass = toi;
			menuManager = new MenuManager(this);
			menuManager.addMenu(new MainMenu(this, new menuImage()));
			//menuManager.addMenu(new GameOverMenu(this, new menuImage()));
			menuManager.addMenu(new PauseMenu(this, new pause()));
			menuManager.FindMenu("Main");
		}
		
		public function LoadGame():void
		{
			isPaused = false;
			init();
			LoadLevel();
			topClass.addChild(canvasBitmap);
			topClass.stage.focus=topClass.stage; 
		}
		
		private function init():void
		{
			backGroundBitmapData = new BitmapData(640, 480, false, 0x000000);
			canvasBitmapData = new BitmapData(640, 480, false, 0xffffff);
			canvasBitmap = new Bitmap(canvasBitmapData);
			camera = new Camera(this, 640, 480, 64);
		}
		
		private function LoadLevel():void
		{
			var loader:XMLLoader = new XMLLoader();
			loader.addEventListener(XMLEvent.XMLLOADED, loading);
			loader.load("Data/levelList.xml");
		}
		
		private function loading(e:XMLEvent):void
		{		
			var xml:XML = new XML(e.Data);
			for(var index:int = 0; index < xml.level.length(); index++)
			{
				var level:Level = new Level(this,xml.level[index].attribute("xmlLocation").toString(),(int)(xml.level[index].attribute("id").toString()));
				levels.push(level);
			}
			currentLevel = levels[0];
			hero = new Player(currentLevel, xml.player.attribute("xmlLocation").toString());
		}
		
		public function Update(deltaTime:int):void
		{
			if(!isPaused || !isGameOver)
			{
				if(currentLevel != null)
				{
					canvasBitmapData.copyPixels(backGroundBitmapData,backGroundBitmapData.rect, new Point(0,0)); //Blit Background of Current Level
					hero.Update(deltaTime);
					camera.Update(deltaTime);
					canvasBitmapData = hero.Draw(canvasBitmapData);	
				}
			}
			else
			{
				menuManager.Update(deltaTime);
			}
			
			if(isGameOver)
			{
				EndGame();
			}
		}
		
		public function KeyHandling(e:KeyboardEvent):void
		{
			if(!isPaused)
			{
				switch(e.keyCode)
				{
					case Key.ESCAPE:
						PauseGame();
						menuManager.FindMenu("Main");
						menuManager.Show();
						break;
					case Key.P:
						PauseGame();
						menuManager.FindMenu("Pause");
						menuManager.Show();
						break;
				}
			}
			else
			{
				menuManager.HandleInput(e.keyCode);
			}
		}
		
		public function PauseGame():void
		{
			isPaused = true;
			canvasBitmap.visible = false;
		}
		
		public function UnPauseGame():void
		{
			isPaused = false;
			canvasBitmap.visible = true;
			topClass.stage.focus = topClass.stage;
			menuManager.Hide();
		}	
		
		public function EndGame():void
		{
			topClass.removeChild(canvasBitmap);
			levels.length = 0;
			menuManager.FindMenu("GameOver");
			menuManager.Show();
		}
	}
}
